package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.OrderDetailBo;
import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.service.MarketOrderService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.Express;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/order")
public class AdminOrderController extends HttpServlet {
    @Autowired
    MarketOrderService orderService ;

    @GetMapping("/channel")
    private Object channel() {
        List<Express> expressesList = orderService.channel();
        return ResponseUtil.ok(expressesList);

    }

    @GetMapping("detail")
    private Object detail(String id) throws IOException {

        int idInt = 0;
        try {
            idInt = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            System.out.println("输入格式错误");
        }
        OrderDetailBo orderDetailBo = orderService.detail(idInt);
        return ResponseUtil.ok(orderDetailBo);

    }

    @GetMapping("list")
    private Object list(String page, String limit, String sort, String order, String orderStatusArray, String start, String end, String userId,String orderSn) throws IOException {
        List<MarketOrder> marketOrderList = orderService.list(page, limit, sort, order, orderStatusArray, start, end, userId,orderSn);
        return ResponseUtil.okList(marketOrderList);

    }

    //回复
    @PostMapping("replay")
    private Object replay(@RequestBody Map<String, String> map) throws IOException {
        String commentId = map.get("commentId");
        String content = map.get("content");
        int commentIdInput = Integer.parseInt(commentId);
        boolean reply = orderService.reply(commentIdInput, content);
        if (reply) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail(620, "回复失败");
        }

    }
    //发货
    @PostMapping("ship")
    private Object shipping(@RequestBody Map<String,String> map) throws IOException {

        String id = map.get( "orderId");
        String shipChannel = map.get("shipChannel");
        String shipSn = map.get( "shipSn");
        int orderId = 0;
        try {
            orderId = Integer.parseInt(id);
            Boolean status = orderService.ship(orderId, shipChannel, shipSn);
            if (status) {
                return ResponseUtil.ok();

            } else {
                return ResponseUtil.fail(620, "订单未付款");
            }
        } catch (NumberFormatException e) {
            System.out.println("输入格式错误");
            return ResponseUtil.fail(620, e.getMessage());
        }
    }

    //退款
    @PostMapping("refund")
    private Object refund(@RequestBody Map<String,String> map) throws IOException {
        /*{"orderId":19,"refundMoney":395}*/

        String id = map.get( "orderId");
        String money =map.get("refundMoney");
        try {
            int orderId = Integer.parseInt(id);
            int refundMoney = Integer.parseInt(money);
            Boolean refund = orderService.refund(orderId, refundMoney);
            if (refund) {
               return ResponseUtil.ok();

            } else {
                return ResponseUtil.fail(620, "订单不能确认收货");
            }
        } catch (Exception e) {
            System.out.println("输入格式错误");
            return ResponseUtil.fail(620, e.getMessage());
        }
    }

    //删除订单
    @PostMapping("delete")
    private Object delete(@RequestBody Map<String,String> map) throws IOException {
        String id = (String)map.get( "orderId");

        int orderId = 0;
        try {
            orderId = Integer.parseInt(id);
            System.out.println("orderId = " + orderId);
            Object status = null;
            Boolean delete = orderService.delete(orderId);
            if (delete) {
                status = ResponseUtil.ok();
            } else {
                status = ResponseUtil.fail(623, "订单不能删除");
            }
           return status;
        } catch (NumberFormatException e) {
            System.out.println("输入格式错误");
            return ResponseUtil.fail(620, e.getMessage());
        }
    }
}
